#!/bin/sh

BASENAME=`basename $0`
MONITOR="false"
DETACHED_OPTION=""
COOKIE="cookie"
ERL=@ERLANG@

usage() {
   echo "Usage: $BASENAME [options]";
}

for _option
do
   # If the previous option needs an argument, assign it.
   if test -n "$_prev"; then
      eval "$_prev=\$_option"
      _prev=
      continue
   fi

   case "$_option" in
   -*=*) _optarg=`echo "$_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
   *) _optarg= ;;
   esac

   case "$_option" in

   --cookie=*)
      COOKIE="$_optarg" ;;

   --detached | -d)
      DETACHED_OPTION="-detached" ;;

   --help | -h)
      usage
      cat << EOF

Options:
      --cookie=COOKIE   use this cookie
   -d,--detached        starts the service detached from the system console
   -h,--help            display this message
   -m,--monitor         run monitor

EOF
   exit 0;;

   --monitor | -m)
      MONITOR="true" ;;

   --* | -* ) {
      echo "$BASENAME: illegal option $_option; use --help to show usage" 1>&2;
      exit 1;
      };;
   * )
      args="$args$_option " ;;

   esac
done

cd `dirname $0`;
ROOT=`dirname "$PWD" | sed -e 's, ,\\\\ ,g'`;
cd "$PWD";

ERL_OPTS="
    -setcookie $COOKIE \
    -pa $ROOT/ebin \
    -sname urlfetch \
    -s urlfetch"

if [ "$MONITOR" = "false" ]; then
    eval $ERL $ERL_OPTS $DETACHED_OPTION start -noshell
else
    eval $ERL $ERL_OPTS -boot start_sasl -run appmon
fi

exit 0;
